package com.sttk.secreconeclick.bookkeep.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.lzb.basic.dialog.BaseSmartDialog;
import com.lzb.basic.utils.SPUtil;
import com.sttk.secreconeclick.bookkeep.Content;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.databinding.DialogChangeCycleBinding;
import com.sttk.secreconeclick.bookkeep.databinding.DialogMoneyColorBinding;

public class MoneyColorDialog extends BaseSmartDialog<DialogMoneyColorBinding> implements View.OnClickListener {


    public MoneyColorDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_money_color;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.llGreen.setOnClickListener(this);
        mDataBinding.llRed.setOnClickListener(this);

        clearSelection();
        int pos = SPUtil.getInt(getContext(), Content.moneyColor, 0);
        switch (pos) {
            case 0:
                mDataBinding.ivGreenSel.setImageResource(R.drawable.yes);
                break;
            case 1:
                mDataBinding.ivRedSel.setImageResource(R.drawable.yes);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        clearSelection();
        if (id == R.id.llGreen) {
            mDataBinding.ivGreenSel.setImageResource(R.drawable.yes);
            dismiss();
            if (listener != null) {
                listener.clickItem(0);
            }
        } else if (id == R.id.llRed) {
            mDataBinding.ivRedSel.setImageResource(R.drawable.yes);
            dismiss();
            if (listener != null) {
                listener.clickItem(1);
            }
        }
    }

    private void clearSelection() {
        mDataBinding.ivGreenSel.setImageResource(R.drawable.no);
        mDataBinding.ivRedSel.setImageResource(R.drawable.no);
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickItem(int pos);
    }
}
